home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / updown.arc / UPDOWN.M < prev   
Text File  |  1987-06-08  |  7KB  |  273 lines

  1. ;**
  2. ;**        BRIEF -- Basic Reconfigurable Interactive Editing Facility
  3. ;**
  4. ;**        Written by Dave Nanian and Michael Strickman.
  5. ;**
  6.  
  7. ;**
  8. ;**        updown.m:
  9. ;**
  10. ;**        This macro file contains routines that upper and lowercase a block.
  11. ;**    these macros are a good example of how to write a block function.  They
  12. ;**    deal with all the known special cases, including tab fields and a
  13. ;**    "percent complete" display that compensates for the size of an integer.
  14. ;**
  15. ;**        The key assignments are as follows:
  16. ;**
  17. ;**        toupper:            Uppercases a block.  Assigned to Ctrl-F6.
  18. ;**
  19. ;**        tolower:            Lowercases a block.  Assigned to Ctrl-F5.
  20. ;**
  21. ;**        Revision history:
  22. ;**        -----------------
  23. ;**        1 December 1986        Rewrote to compensate for slight problem with
  24. ;**                                    BRIEF v1.33 and earlier versions.  Also
  25. ;**                                    optimized for speed.  Thanks to Michael Hannah
  26. ;**                                    who pointed out the speed problems in the first
  27. ;**                                    place.
  28. ;**
  29. ;**        25 May 1987                Modified assigned keys because BRIEF 2.0 uses
  30. ;**                                    Ctrl-F5 and Ctrl-F6 for other purposes. 
  31. ;**                                    Started to use <Alt-9> as the equivalent of
  32. ;**                                    SmartKey's SuperShift.
  33. ;**                                    Used BRIEF 2.0's parameters for inq-marked.
  34. ;**                                    Added code for non-inclusive marks.
  35. ;**                                    Added an error message for column marks.
  36. ;**                                    Revised by Lew Paper
  37.  
  38. (macro updown
  39.     (
  40.         (assign_to_key "#-32768#108" "tolower")        ; <Alt-9> Lower case L.  LP
  41.         (assign_to_key "#-32768#117" "toupper")        ; <Alt-9> u.  LP
  42.  
  43. ; LP    (assign_to_key "%#98" "tolower")                    ;** Assigned to Ctrl-F5
  44. ; LP    (assign_to_key "%#99" "toupper")                    ;** Assigned to Ctrl-F6
  45.     )
  46. )
  47.  
  48.  
  49. ;**
  50. ;**        _block_case:
  51. ;**
  52. ;**        This generic function upper or lower cases a block, depending on
  53. ;**    the value of the first parameter.  Non-zero upper cases a block, zero
  54. ;**    lower cases it.
  55. ;**
  56. ;**        If no block is marked, the current line is "cased".
  57. ;**
  58.  
  59. (macro _block_case
  60.     (
  61.         (string    before
  62.                     after
  63.         )
  64.         (int    start_line
  65.                 start_col
  66.                 end_line
  67.                 end_col
  68.                 block_type                        ; LP
  69.                 curr_line
  70.                 curr_col                            ; LP
  71.                 num_lines
  72.                 do_upper
  73.                 scale_factor
  74.                 num_chars
  75.                 before_line
  76.                 before_col
  77.                 after_line
  78.                 after_col
  79.                 line
  80.                 col
  81.                 done
  82.         )
  83.         (message "Case converting block...")
  84.         (get_parm 0 do_upper)
  85.         (save_position)
  86.  
  87.         (= block_type (inq_marked start_line start_col end_line end_col))    ; LP
  88.                                                     ; Note that start always precedes end
  89.                                                     ; and end_col for line mark is 20736,
  90.                                                     ; while start_col is always correct
  91.  
  92.         (if (== block_type 2)                ; Column mark.  LP
  93.             (                                        ; LP
  94.                  (message "Can not convert column blocks yet")    ; LP
  95.                 (return 1)                        ; Dummy value  ; LP
  96.             )                                        ; LP
  97.         )                                            ; (if (== block_type 2) LP
  98.  
  99.         (if (! block_type)                    ; LP
  100. ; LP    (if (! (inq_marked))
  101.             (
  102.                 (inq_position start_line)
  103.                 (= start_col 1)
  104.                 (end_of_line)
  105.                 (inq_position end_line end_col)
  106.                 (beginning_of_line)
  107.             )
  108.         ;else
  109.             (
  110.                 (raise_anchor)                    ; Moved here to show that we are
  111.                                                     ; done with the original block.  LP
  112.                 (if (== block_type 4)        ; Non-inclusive mark.  LP
  113.                     (                                ; LP
  114.                         (if (&& (== start_line end_line) (== start_col end_col))
  115.                                                     ; Original block had only 1 character. 
  116.                                                     ; LP
  117.                             (                        ; LP
  118.                                 (message "nothing to do")
  119.                                 (return 1)        ; Throw away value
  120.                             )                        ; LP
  121.                         )                            ; (if (&& (== start_line ... LP
  122.                         (inq_position curr_line curr_col)    ; LP
  123.                         (if (|| (!= curr_line start_line) (!= curr_col start_col))
  124.                                                     ; Block marked in the upper left hand
  125.                                                     ; corner, so skip the last character.  LP
  126.                             (                        ; LP
  127.                                 (move_abs end_line end_col)    ; LP
  128.                                 (prev_char)        ; LP
  129.                                 (inq_position end_line end_col)
  130.                             )                        ; LP
  131.                         ;else                        ; Block marked in the lower right hand
  132.                                                     ; corner, so skip the first character.  LP
  133.                             (                        ; LP
  134.                                 (move_abs start_line start_col)    ; LP
  135.                                 (next_char)        ; LP
  136.                                 (inq_position start_line start_col)    ; LP
  137.                             )                        ; LP
  138.                         )                            ; (if (|| (!= curr_line...  LP
  139.                     )
  140.                 )                                    ; (if (== block_type 4)  LP
  141.                 (move_abs start_line start_col)    ; LP
  142. ; LP            (inq_position start_line start_col)
  143. ; LP            (swap_anchor)
  144. ; LP            (inq_position end_line end_col)
  145.  
  146. ; LP            (if (|| (< end_line start_line) (&& (== start_line end_line) (< end_col start_col)))
  147. ; LP                (
  148. ; LP                    (int    temp)
  149. ; LP                    (= temp end_line)
  150. ; LP                    (= end_line start_line)
  151. ; LP                    (= start_line temp)
  152. ; LP                    (= temp end_col)
  153. ; LP                    (= end_col start_col)
  154. ; LP                    (= start_col temp)
  155. ; LP                )
  156. ; LP            ;else
  157. ; LP                (swap_anchor)
  158. ; LP                (raise_anchor)
  159.             )                                        ; ;else
  160.         )                                             ; (if ! block_type)
  161.         (= num_lines (+ (- end_line start_line) 1))
  162.         (= curr_line start_line)
  163.  
  164.         (if (> num_lines 100)
  165.             (= scale_factor (/ 32767 num_lines))
  166.         ;else
  167.             (= scale_factor 100)
  168.         )
  169.         (while (<= curr_line end_line)
  170.             (
  171.                 (if (&& (!= curr_line end_line) (!= curr_line start_line))
  172.                     (= before (read))
  173.                 ;else
  174.                     (
  175.                         (inq_position before_line before_col)
  176.                         (prev_char)
  177.                         (next_char)
  178.                         (inq_position after_line after_col)
  179.  
  180.                         (if (|| (!= before_line after_line) (> after_col before_col))
  181.                             (prev_char)
  182.                         )
  183.                         (save_position)
  184.                         (end_of_line)
  185.                         (inq_position NULL after_col)
  186.                         (restore_position)
  187.  
  188.                         (if (|| (!= curr_line end_line) (<= after_col end_col))
  189.                             (= before (read))
  190.                         ;else
  191.                             (
  192.                                 (save_position)
  193.  
  194.                                 (while (! done)
  195.                                     (
  196.                                         (++ num_chars)
  197.                                         (next_char)
  198.                                         (inq_position line col)
  199.                                         (= done (|| (!= line end_line) (> col end_col)))
  200.                                     )
  201.                                 )
  202.                                 (restore_position)
  203.                                 (= before (read num_chars))
  204.                             )
  205.                         )
  206.                     )
  207.                 )
  208.                 (if do_upper
  209.                     (= after (upper before))
  210.                 ;else
  211.                     (= after (lower before))
  212.                 )
  213.                 (if (!= after before)
  214.                     (
  215.                         (if (index after "\n")
  216.                             (
  217.                                 (= after (substr after 1 (- (strlen after) 1)))
  218.                                 (delete_to_eol)
  219.                             )
  220.                         ;else
  221.                             (
  222.                                 (drop_anchor)
  223.                                 (= num_chars (strlen after))
  224.  
  225.                                 (while (> (-- num_chars) 0)
  226.                                     (next_char)
  227.                                 )
  228.                                 (delete_block)
  229.                             )
  230.                         )
  231.                         (insert after)
  232.                     )
  233.                 )
  234.                 (move_abs (++ curr_line) 1)
  235.  
  236.                 ;**
  237.                 ;**        This rather messy calculation scales things so that
  238.                 ;**    we get as much granularity as possible when computing
  239.                 ;**    percentages without overflowing an integer.
  240.                 ;**
  241.  
  242.                 (message "Case converting block, %d%% complete..."
  243.                             (/ (* 100 (/ (* (- curr_line start_line) scale_factor) num_lines)) scale_factor))
  244.             )
  245.         )
  246.         (restore_position)
  247.         (message "Case conversion completed.")
  248.     )
  249. )
  250.  
  251. ;**
  252. ;**        toupper:
  253. ;**
  254. ;**        This simple macro calls _block_case with the parameter that means
  255. ;**    "Hey, guy, uppercase this block!"
  256. ;**
  257.  
  258. (macro toupper
  259.     (_block_case 1)
  260. )
  261.  
  262. ;**
  263. ;**        tolower:
  264. ;**
  265. ;**        This simple macro calls _block_case with the parameter that means
  266. ;**    "Hey, guy, lowercase this block!"
  267. ;**
  268.  
  269. (macro tolower
  270.     (_block_case 0)
  271. )
  272.                                  
  273.